package com.yonghong.groupreport.controller;


import com.alibaba.fastjson2.JSON;
import com.yonghong.groupreport.config.DynamicDataSource;
import com.yonghong.groupreport.domain.PaOrganize;
import com.yonghong.groupreport.service.IPaOrganizeService;
import com.yonghong.groupreport.service.impl.PaOrganizeServiceImpl;
import com.yonghong.groupreport.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;
import com.yonghong.groupreport.utils.BaseController;
@RestController
@RequestMapping("reports/organize")
public class PaOrganizeController extends  BaseController {
    @Autowired
    private IPaOrganizeService paOrganizeService;
    @Autowired
    private DynamicDataSource dynamicDataSource;

    /**
     * 新增组织信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException {
        if(ajaxResult.get("msg").toString()!=null)
        {
            DynamicDataSource.clearDataSource();
        // 切换数据源
        dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }
        PaOrganize paOrganize= JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")),PaOrganize.class);

//        PaOrganize paOrganize= (PaOrganize) ajaxResult.get("paOrganize");


        return toAjax(paOrganizeService.insertPaOrganize(paOrganize));

    }
    /**
     * 修改组织信息
     */

    @PutMapping
    public AjaxResult edit(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException {

        if(ajaxResult.get("msg").toString()!=null)
        {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }
        PaOrganize paOrganize=JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")),PaOrganize.class);

        return toAjax(paOrganizeService.updatePaOrganize(paOrganize));
    }

    /**
     * 删除组织信息
     */

    @DeleteMapping
    public AjaxResult remove(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException, JSONException {
       int rows = 0;
        if (ajaxResult.get("msg") != null) {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }


            JSONArray jsonArr = new JSONArray(ajaxResult.get("data").toString()); // 创建JSONArray对象并传入JSON字符串

            int length = jsonArr.length(); // 获取JSON数组长度
            Long[] orgIds = new Long[length]; // 定义存放结果的long类型数组

            for (int i = 0; i < length; i++) {
                orgIds[i] = jsonArr.getLong(i); // 遍历JSON数组，依次提取每个元素并赋值到long数组中
            }

        //NC

//        if(ajaxResult.get("msg")=="NC"){
//
//
//        }
//        else {

             rows=paOrganizeService.deletePaOrganizeByOrgIds(orgIds);
//        }
        return toAjax(rows);
    }

}
